layui.define(['table', 'form', 'element','selectN'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form, element = layui.element,selectN = layui.selectN;

  var placeId = $('input[name="id"]').val();

    table.render({
        elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/place/business_config/list.data'
        ,where:{placeId:placeId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'businessType', title: '业务Code'}
            ,{field: 'businessTypeName', title: '业务名称'}
            ,{field: 'feeTemplateId', title: '价格配置', templet: function (record){
                    if(record.feeTemplateId){
                        return record.feeTemplateTitle + '(ID:'+record.feeTemplateId+')';
                    }else {
                        return '全局模版';
                    }
                }}
            ,{field: 'deviceCount', title: '设备数量'}
            ,{field: 'updateDate', title: '最后修改时间'}
            ,{field: 'createDate', title: '创建时间'}
            ,{title: '操作', width: 160, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
        ]]
        ,page: false
        ,height: 'full'
        ,text: '对不起，加载出现异常！'
    });

    table.render({
        elem: '#LAY-table-user'
        ,url: layui.setter.webRoot + '/place/user/list.data'
        ,where:{placeId:placeId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'userId', title: '用户ID'}
            ,{field: 'userName', title: '用户名称'}
            ,{field: 'userPhone', title: '用户手机号'}
            ,{field: 'roleTypeName', title: '关联角色'}
            ,{field: 'incomeRatioNote', title: '分成比例'}
            ,{field: 'note', title: '备注'}
            ,{title: '操作', width: 140, align:'center', fixed: 'right', templet:function(record){
                var html = '';
                if(record.canUpdate){
                    html = html + '<a class="layui-btn layui-btn-xs" lay-event="userInfo">查看</a>';
                    html = html + '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delUser">删除</a>';
                }
                return html;
                }}
        ]]
        ,page: false
        ,height: 'full'
        ,text: '对不起，加载出现异常！'
    });
  
  //事件
  var active = {
      updatePlace:function (){
          layer.open({
              type: 2
              ,title: '修改实体店网点'
              ,content: layui.setter.webRoot + '/place/physical_store/update.html?id='+placeId
              ,maxmin: true
              ,area: ['80%', '97%']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/place/physical_store/update.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  layer.close(index);
                                  location.reload();
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,mailAddress:function (){
          layer.open({
              type: 2
              ,title: '邮件地址'
              ,content: layui.setter.webRoot + '/place/physical_store/mail.html?id='+placeId
              ,maxmin: true
              ,area: ['80%', '500px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/place/physical_store/mail/save.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  layer.close(index);
                                  layer.msg('保存成功',{icon: 1});
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,addBusinessConfig: function(){
          layer.open({
              type: 2
              ,title: '新增业务类型'
              ,content: layui.setter.webRoot + '/place/business_config/add.html?placeId=' + placeId
              ,maxmin: true
              ,area: ['400px', '500px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field; //获取提交的字段
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/place/business_config/add.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  layer.close(index);
                                  table.reload('LAY-table');
                                  layer.msg('新增成功',{icon: 1});
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,delBusinessConfig:function(obj){
          layer.confirm('确定要删除该业务类型吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/place/business_config/del.do',
                  data:{placeId:placeId,businessType:obj.data.businessType},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          obj.del();
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
      ,costConfig: function (obj) {
          parent.layer.open({
              type: 2
              , title: '配置业务服务价格'
              , content: layui.setter.webRoot + '/place/business_config/cost.html?placeId='+placeId+"&businessType="+obj.data.businessType
              , maxmin: false
              , area: ['95%', '95%']
              , btn: ['取消']
          });
      }
      ,addUser: function(){
          layer.open({
              type: 2
              ,title: '新增用户'
              ,content: layui.setter.webRoot + '/place/user/add.html?placeId=' + placeId
              ,maxmin: true
              ,area: ['800px', '450px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field; //获取提交的字段
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/place/user/add.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  layer.close(index);
                                  table.reload('LAY-table-user');
                                  layer.msg('新增成功',{icon: 1});
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,userInfo: function(obj){
          layer.open({
              type: 2
              ,title: '用户信息'
              ,content: layui.setter.webRoot + '/place/user/info.html?placeId=' + placeId + '&userId='+obj.data.userId
              ,maxmin: true
              ,area: ['85%', '85%']
              ,btn: ['取消']
          });
      }
      ,delUser:function(obj){
          layer.confirm('确定要删除该用户吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/place/user/del.do',
                  data:{placeId:placeId,userId:obj.data.userId},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          obj.del();
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
  }

  element.on('tab(tab-info)', function (data) {
    if (data.index == 0) {
        table.reload('LAY-table')
    }else if(data.index == 1) {
        table.reload('LAY-table-user')
    }
  });
  
  $('.layui-btn.LAY-btn-fun').on('click', function () {
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
  });

    table.on('tool(LAY-table)', function (obj) {
        var type = obj.event;
        active[type] ? active[type].call(this, obj) : '';
    });

    table.on('tool(LAY-table-user)', function (obj) {
        var type = obj.event;
        active[type] ? active[type].call(this, obj) : '';
    });
  
  exports('physical_store_info', {})
});